<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Alarm Realtime</title>
	<link rel="stylesheet" href="../js/easyui/themes/bootstrap/easyui.css">
	<link rel="stylesheet" href="../js/easyui/themes/icon.css">
	<!--link rel="stylesheet" href="../css/index-ui.css" /-->
	<link rel="stylesheet" href="../css/eq-config.css" />
	<script type="text/javascript" src="../js/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="../js/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../js/ams.util.js"></script>
	<script type="text/javascript" src="../js/webapi.js"></script>
	<script type="text/javascript" src="../js2/loginCookies.js"></script>
</head>

<body onload="on_page_load()">
<table id="eq_rt_alarm_grid" class="easyui-datagrid" title="" style="width:100%;height:100%"
	data-options="ctrlSelect:true,rownumbers:true,singleSelect:true,autoRowHeight:'true'">
<thead>
	<tr>
		<!--<th data-options="field:'mod',width:100,align:'left',formatter:formatPrice">Module</th>-->
		<th data-options="field:'mod',width:100,align:'left'">Module</th>
		<th data-options="field:'id',width:50,align:'left'	">ID</th>
		<th data-options="field:'desc',width:690,align:'left'">Alarm Description</th>
		<th data-options="field:'dt',width:120">DateTime</th>
		<th data-options="field:'level',hidden:true,width:20">Level</th>
	</tr>
</thead>
</table>
</body>
<script language="javascript">

function on_page_load()
{	
	$('#eq_rt_alarm_grid').datagrid({
		onDblClickRow: function(rowIndex, rowData){
			onAlarmDetailInfo(rowData);
		}
	})
/*
	$('#eq_rt_alarm_grid').datagrid({
		onSelect:function(rowIndex, rowData){
			var n = 2;
		}
	})
	
	$('#eq_rt_alarm_grid').datagrid({
		onUnselectAll:function(rowIndex, rowData){
			var n = 2;
		}
	})
*/

	update();
}

function update(){
	//$('#eq_rt_alarm_grid').datagrid('loadData', { total: 0, rows: [] });
	
	if (ams_is_sim_mode()) {
		$.getJSON('almRt.json', 
			function(json_data) {
				$('#eq_rt_alarm_grid').datagrid({
					data : json_data
				})
			})
	} else {
		var eqn = get_page_param('eq')
		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("getEquipRTAlarms"),	
			data:JSON.stringify({ 'eqName': eqn}),
			dataType: 'json',
			success : function(json_data) {
				console.log(JSON.parse(json_data.d))
				$(eq_rt_alarm_grid).datagrid({
					data : JSON.parse(json_data.d)
				})
			},
			error: function (response) {
				$.messager.alert("Error", "Error2: " + response.responseText, "error");
			}
		});
	}
	setTimeout('update()', 5000)
}

function formatPrice(val,row){
	if (val.length > 0){
		return '<span style="color:blue;">' + val + '</span>';
	} else {
		return val;
	}
}

$('#eq_rt_alarm_grid').datagrid({
    rowStyler:function(index, row){
    	if (row.level == "0"){
    		return 'background-color:yellow;';
    	}else if (row.level == "1"){
    		return 'background-color:yellow;';
    	}else if (row.level == "2"){
    		return 'background-color:red;';
    	}else if (row.level == "3"){
    		return 'background-color:#FFA500';
    	}
    }
});

function onAlarmDetailInfo(row){
	if (ams_is_sim_mode()) {
		$.messager.alert("Tip", "Alarm desc.", "info");
		return;
	}
	
	var eqn = get_page_param('eq')
  $.ajax({
		type: 'POST',
		contentType: 'application/json',
		url: getWebServiceUrl("getEqAlarmOpTip"),
		data:JSON.stringify({
			"eqName":eqn,
			"almId":row.id
		}),
		dataType: 'json',
		success: function (response) {
			console.log(JSON.parse(response.d))
			var icon = "info";
     	if (row.level == "Inform"){
     		icon = "info";
     	}
     	else if (row.level == "Warn"){
     		icon = "warning";
     	}
     	else{
     		icon = "error";
     	}
     	var jsonData = JSON.parse(response.d);
     	var title = jsonData.title;
     	var info = "";
     	if (title == "" && jsonData.rows.length == 0){
     		title = "注意";
     		info = "此报警没有详细描述信息。";
     	}
     	else{
     		for (var i = 0; i < jsonData.rows.length; i++){
	     		if (i > 0){
	     			info += "<br>";
	     		}        		
	     		info += jsonData.rows[i].info;
     		}
     	}
     	
     	$.messager.alert(title, info, icon);
		},
		error: function (error) {
			$.messager.alert("Error", "Error: " + error, "error");
		}
	});
}
</script>

</html>
